const query = require('../config/sql')

const system = {
    getUserList: async (ctx, next) => {
        const sql = `select * from users where isadmin="0";`
        await query(sql).then(res => {
            if (res) {
                ctx.body = {
                    code: 200,
                    data: res
                }
            }
        })
    },
    logoff: async (ctx, next) => {
        const { user_id } = ctx.request.body
        const sql1 = `DELETE FROM users WHERE user_id =?;`
        const sql2 = `DELETE FROM cardlist WHERE publisher_id =?;`

        await query(sql1, [user_id])
        await query(sql2, [user_id])
        ctx.body = {
            code: 200,
            msg: '注销成功！'
        }
    },
    getuserPermissions: async (ctx, next) => {
        const { user_id } = ctx.request.body
        const sql = `SELECT * FROM permissions WHERE user_id =?;`
        await query(sql, [user_id]).then(result => {
            ctx.body = {
                code: 200,
                data: result[0]
            }
        })
    },
    setuserPermissions: async (ctx, next) => {
        const { permissionId, user_id, publish, comment, approval } = ctx.request.body
        const sql = `update  permissions set publish =?, comment =?, approval=? WHERE user_id =? and permissionId=?;`
        await query(sql, [publish, comment, approval, user_id, permissionId]).then(result => {
            if (result) {
                ctx.body = {
                    code: 200,
                    message: '权限配置成功！'
                }
            }

        })
    },

    getcomment: async (ctx, next) => {
        const sql = `select * from commentinfo;`
        await query(sql).then(result => {
            if (result) {
                ctx.body = {
                    code: 200,
                    message: '获取评论成功！',
                    data: result
                }
            }
        })
    },
    getWhistleblowmanagement: async (ctx, next) => {
        const { type } = ctx.request.body
        let sql;
        if (type == 'comment') {
            sql = `SELECT w.*, u.nickname as Whistleblowers,c.content,c.user_id,u2.nickname
                  FROM whistleblow w
                  JOIN users u ON w.WhistleblowerId = u.user_id
                  JOIN commentinfo c ON w.ReportsId = c.comment_id
                  LEFT JOIN users u2 ON w.WhistleblowerId = c.user_id
                  WHERE w.Whistleblowtype = ?;`
        }
        if (type == 'pet') {
            sql = `SELECT w.*, u.nickname,c.remark,c.publisher_id,c.type as petType,c.name,c.gender,u2.nickname as Whistleblowers
                  FROM whistleblow w
                  JOIN users u ON w.WhistleblowerId = u.user_id
                  JOIN cardlist c ON w.ReportsId = c.card_id
                  LEFT JOIN users u2 ON c.publisher_id = u2.user_id
                  WHERE w.Whistleblowtype = ?;`
        }
        if (type == 'share') {
            sql=`SELECT w.* ,u.nickname as Whistleblowers,s.title,s.content,s.sharer_id,u2.nickname 
             FROM whistleblow w 
            JOIN users u ON w.WhistleblowerId = u.user_id
            JOIN shareposts s ON w.ReportsId = s.shareposts_id
            LEFT JOIN users u2 ON s.sharer_id = u2.user_id 
            WHERE w.Whistleblowtype = ?;`
        }
        await query(sql, [type]).then(result => {
            if (result) {
                ctx.body = {
                    code: 200,
                    message: '获取举报列表成功！',
                    data: result
                }
            }
        }).catch(err => {
            console.log(err)
        })

    }

}

module.exports = system